




C Preprocessor Test 1

1) In which stage the below code gets replaced by the contents of the file #include<stdio.h>

During linking
During editing
During preprocessing
During execution

Show Answer

The correct option is (c).
Explanation:
During preprocessing stage the line #include<stdio.h> with the system header file of that name gets replaced by the contents of file  stdio.h.
Therefore the entire text of the file 'stdio.h' replaces with #include directive.

2) C preprocessor directive #undef can be used with a macro that has been #define earlier.

True
False

Show Answer

The correct option is (a).
Explanation:
True, the directive #undef can be used only with a macro that has been #define earlier in a program.
For Example: #define PI 3.14
We can undefine PI macro by #undef PI

3) C preprocessor directive #ifdef...#elif?#endif is used for conditional compilation.

True
False

Show Answer

The correct option is (a).
Explanation:
True, the C macros like #ifdef...#elif?#endif are used for performing conditional operation in C program.
The syntax of C preprocessor directive is:

#if 
#elif 
#endif


4) What will be the output of the below program?

#include
#define SWAP(x, y) int t; t=x, x=y, y=t;
int main()
{
    int x=10, y=20;
    SWAP(x, y);
    printf("x = %d, y = %d\n", x, y);
    return 0;
}


x=10 , y=20
x=20, y=10
Error: Undefined symbol 't'
Error: Declaration not allowed in macro

Show Answer

The correct option is (b).
Explanation:
The macro statement SWAP(x, y) int t; t=x, x=y, y=t; swaps the value of given two variable.
Step 1: int x=10, y=20; The variable x and y are declared as an integer type and initialized to 10, 20 respectively.
Step 2: SWAP(x, y);. Here the macro is substituted and it swaps the value to variable x and y.
Hence the output of the program is  x=20, y=10.

5) Which of the following are correctly formed #define statements in C language?

#define CUBE(x) (X*X*X)
#define CUBE(X) {X*X*X}
#define CUBE (X) X*X*X
#define CUBE(X) (X)*(X)*(X)

Show Answer

The correct option is (d).
Explanation:
The syntax for macro definition with argument is:

"#define MACRO_NAME(ARG) (ARG*ARG*ARG) "


There should be no space between macro's name and it's '(args)'.
The variables used as macro's argument are case sensitive and its expansion should be same. i.e. 'x' and 'X' are different variables.
A macro expansion should be enclosed within parenthesis '(    )' (do not use { } or [ ]).














Please Share





